
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include "cmd.h"

#include "wrapperjson.h"
#include "./../3rdparty/include/json-c/json.h"
#include "cli_main.h"

const I32 minCell = 0;
const I32 maxCell = 13;
const char *codeStr="code";
const char *typeStr="type";
const char *timestampStr="timestamp";
const char *dataStr="data";
const char *cellNumberStr="cellNumber";
const char *bandStr="band";
const char *dlEarfcnStr="dlEarfcn";
const char *ulEarfcnStr="ulEarfcn";
const char *pciStr="pci";
const char *plmnidStr="plmnid";
const char *tacStr="tac";
const char *neighborCellListStr="neighborCellList";
const char *freqStr="freq";
const char *tacUpdateCycleStr="tacUpdateCycle";
const char *cycleStr="cycle";
const char *GpsDelayStr="GpsDelay";
const char *IsMeasureEnableStr="IsMeasureEnable";
const char *BandWidthStr="BandWidth";
const char *SyncModeStr="SyncMode";
const char *IsSaveFsStr="IsSaveFs";
const char *statsStr="stats";
const char *resultStr="result";
const char *cmdTypeStr="cmdType";
const char *devNumberStr="devNumber";
const char *devNameStr="devName";
const char *typeModelStr="typeModel";
const char *devTypeStr="devType";
const char *devConformationStr="devConformation";
const char *phoneNumberStr="phoneNumber";
const char *devPosStr="devPos";
const char *heightStr="height";
const char *longitudeStr="longitude";
const char *latitudeStr="latitude";
const char *macStr="mac";
const char *devAddresssStr="devAddresss";
const char *regSuccStr="Device registered successfully";
const char *recCauseStr="reconnectCause";
const char *msgStr="msg";
const char *patempStr="patemp";
const char *paNumberStr="paNumber";
const char *tempStr="temp";
const char *devtempStr="devtemp";
const char *boardtempStr="boardtemp";
const char *wirelessSwitchStr="wirelessSwitch";
const char *syncinfoStr="syncinfo";
const char *syncmethodStr="syncmethod";
const char *syncstatusStr="syncstatus";
const char *paPowerStr="paPower";
const char *powerStr="power";
const char *paWaveRatioStr="paWaveRatio";
const char *paSwitchStr="paSwitch";
const char *cpuStr="cpu";
const char *memoryStr="memsory";
const char *upresultStr="upresult";
const char *srcTimeStr="srcTime";
const char *timeStr="time";
const char *autoBccModeStr="autoBccMode";
const char *isAutoStr="isAuto";
const char *runTimeStr="runTime";
const char *hourStr="hour";
const char *minuteStr="minute";
const char *secondStr="second";
const char *snifferCycleStr="snifferCycle";
const char *mncStr="mnc";
const char *mccStr="mcc";
const char *lacStr="lac";
const char *bccStr="bcc";
const char *arfcnStr="arfcn";
const char *racStr="rac";
const char *cellIdStr="cellId";
const char *autoLacStr="autoLac";
const char *pscStr="psc";
const char *redectStr = "reDetectMinuts";
const char *authSrvStr="authSrvHost";
const char *authPortStr="authPort";
const char *mgr1SrvStr="mgr1SrvHost";
const char *mgr1PortStr="mgr1Port";
const char *up1SrvStr="up1SrvHost";
const char *up1PortStr="up1Port";
const char *localIpStr="localIpAddr";
const char *localPortStr="localPort";
const char *ueIdStr="ueId";
const char *bsicStr="bsic";
const char *rssiStr="rssi";
const char *c1Str="c1";
const char *c2Str="c2";
const char *SnifferCompleteStr="Sniffer Complete";
const char *upgradeUrl="patchUrl";                                                    
const char *upgradeMd5="md5sum";                                                      
const char *upgradePatch="patchInfo";                                                 
const char *upgradeVersion="version"; 
const char *palistStr="palist"; 
const char *blackListStr = "blacklist";
const char *whiteListStr = "whitelist";
const char *imsiStr="imsi";
const char *imeiStr="imei";
const char *tmsiStr="tmsi";
const char *BbuRFStateStr="BbuRFState";
const char *RFSwitchStr="RFSwitch";
const char *ReqTypeStr="ReqType";
const char *ReqCodeStr="ReqCode";
const char *upattStr="upatt";
const char *lowattStr="lowatt";
const char *ftpserverStr="ftpserver";
const char *ftpuserStr="ftpuser";
const char *ftppasswdStr="ftppasswd";
const char *ftpportStr="ftpport";
const char *ftptimerStr="ftptimer";
const char *ftpmaxcountStr="ftpmaxcount";
const char *ftpbsicStr="ftpbsic";
const char *ftpcompanyStr="ftpcompany";
const char *captimeStr = "captime";
const char *seqNumStr = "seqNum";
const char *dateStr = "date";
const char *packetStr = "packet";
const char *crcStr = "crc";
const char *snStr = "sn";
const char *lengthStr = "length";
const char *powerLevelStr = "powerLevel";
typedef struct json_object * JSON;


static void json_add_string(const char * json_name, const char * json_para, JSON json_root)
{
    JSON add_string;
    add_string = json_object_new_string(json_para);
    json_object_object_add(json_root, json_name, add_string);
}
static void json_add_int(const char * json_name, const int json_para, JSON json_root)
{ 
    JSON add_int;
    add_int = json_object_new_int(json_para);
    json_object_object_add(json_root, json_name, add_int);
}
static void json_add_double(const char *json_name, const double json_para, JSON json_root)
{
    JSON add_double;
    add_double = json_object_new_double(json_para);
    json_object_object_add(json_root, json_name, add_double);
}

static void json_add_array(const char * json_name, const int json_para, JSON json_root)
{
    JSON child_node;
    child_node = json_object_new_object();
    json_add_int(json_name, json_para, child_node);
    json_object_array_add(json_root, child_node);

}

static bool json_get_array(char *buf,char *data, int id )
{
    I8 num[DEV_INFO_NUM_LEN];
    memset(num, 0 , DEV_INFO_NUM_LEN);
    JSON sub_elemt, total;
    total = json_tokener_parse(buf);
    enum json_type jsontype;
    if (total == NULL)
    {
        return false;
    } 	
    jsontype = json_object_get_type(total);
    if (jsontype != json_type_array)
    {
        cli_app_log(CLI_LOG_NOTICE, "not array\n");
        return false;
    }

    sub_elemt = json_object_array_get_idx(total, id);
    if (sub_elemt == NULL)
    {
        return false;
    }

    sprintf(data, "%s", json_object_to_json_string(sub_elemt));
    json_object_put(total);     
    return true;

}

bool  wrapperSimpleMsg(I32 type, I32 code, I8* outdata, I32 *len)
{
    JSON cmdRoot;
    cmdRoot = json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool  wrapperTimeSyncMsg(I32 type, I32 code, I32 time, I8* outdata, I32 *len)
{
    JSON cmdRoot;
    cmdRoot = json_object_new_object();
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, time, cmdRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}



bool wrapperwireless(I32 type, I32 code, I32 cell, I8* outdata, I32 *len)
{
    JSON cmdRoot, dataRoot;
    cmdRoot = json_object_new_object();
    dataRoot = json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);

    if ( cell > minCell && cell < maxCell)
    {
        json_add_int(cellNumberStr, cell, dataRoot);
    }

    json_object_object_add(cmdRoot, dataStr, dataRoot);
   
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
   
}

bool wrapperWirelessTwoLevel(I32 type, I32 code, I32 cell, const I8 *dataName, I8* outdata, I32 *len)
{
    JSON cmdRoot, dataRoot, data1Root;
    cmdRoot = json_object_new_object();
    dataRoot = json_object_new_object();
    data1Root = json_object_new_array();
    
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);

    if ( cell >= minCell && cell < maxCell)
    {
        json_add_array(cellNumberStr, cell, data1Root);
    }

    json_object_object_add(dataRoot, dataName, data1Root);
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
   
}

bool wrapperMsgWithData(I32 type, I32 code, I32 value, const I8 *dataName, I8* outdata, I32 *len)
{
    JSON cmdRoot, dataRoot;
    cmdRoot = json_object_new_object();
    dataRoot = json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);
    json_add_int(dataName, value, dataRoot);
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;    
}

bool wrapperImsiList(I32 type, I32 code, const I8 *data, I8* outdata, I32 *len)
{
    I8 imsi[2048] = {0};
    BlackList* list = (BlackList*)data;
	I8 *ptr = list->imsiList;
	
    JSON cmdRoot, dataRoot;
    cmdRoot = json_object_new_object();
    dataRoot = json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);

    int i = 0;
    int imsiLen = 0;
    for(i = 0; i < list->number; i++)
    {
		strcpy(imsi, ptr);
        imsiLen += strlen(ptr); 
        if ( i != (list->number - 1))
            imsi[imsiLen++] = ',';
		ptr += 20;
    }
	
	json_add_string(imsiStr, imsi, dataRoot);    
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;    
}

bool wrapperUpgradeCfg(I32 type, I32 code, const I8 *data, I8* outdata, I32 *len)
{
    UPGRADE_INFO *upgradeInfo = (UPGRADE_INFO  *)data;
	
    JSON cmdRoot, dataRoot,patchRoot;
    cmdRoot = json_object_new_object();
    dataRoot = json_object_new_object();
    patchRoot = json_object_new_object();
	
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);
	
    json_add_string(upgradeUrl, upgradeInfo->url, dataRoot);
    json_add_string(upgradeMd5, upgradeInfo->md5sum, patchRoot);
    json_add_string(upgradeVersion, upgradeInfo->patch, patchRoot);
	
    json_object_object_add(dataRoot, upgradePatch, patchRoot);
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperInterfaceCfg(I32 type, I32 code, const I8 *data, I8* outdata, I32 *len)
{
    NETWORK_CONF_PTR *nwConfig = (NETWORK_CONF_PTR *)data;
    SRV_INFO *srvPtr = nwConfig->mng;
    SRV_INFO *localPtr = nwConfig->local;
    SRV_INFO *dataPtr = nwConfig->data;
	
    JSON cmdRoot, dataRoot;
    cmdRoot = json_object_new_object();
    dataRoot = json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(codeStr, code, cmdRoot);
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);

    if(srvPtr != NULL)
    {
        if(srvPtr->addr[0] != 0)
        {
			json_add_string(mgr1SrvStr, srvPtr->addr, dataRoot);
        }
		
        if(srvPtr->port[0] != 0)
        {
			json_add_string(mgr1PortStr, srvPtr->port, dataRoot);
        }
    }

    if(localPtr!= NULL)
    {
        if(localPtr->addr[0] != 0)
        {
			json_add_string(localIpStr, localPtr->addr, dataRoot);
        }
		
        if(localPtr->port[0] != 0)
        {
			json_add_string(localPortStr, localPtr->port, dataRoot);
        }
    }

    if(dataPtr!= NULL)
    {
        if(dataPtr->addr[0] != 0)
        {
			json_add_string(up1SrvStr, dataPtr->addr, dataRoot);
        }
		
        if(dataPtr->port[0] != 0)
        {
			json_add_string(up1PortStr, dataPtr->port, dataRoot);
        }
    }
			
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperwirelessCfg(WirelessConfig *info, I8 *outdata, I32 *len)
{
    I32 i = 0;
    time_t rawtime;
    time(&rawtime); 

    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    JSON data1Root = json_object_new_object();
    json_add_int(typeStr, CMD_CONFIG, cmdRoot);
    json_add_int(codeStr, CFG_WIRELESS, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);

	for(; i < maxCell - 1; i++)
	{
        cli_app_log(CLI_LOG_DEFAULT, "set wireless config is %d\n", info->wirelessSupported[i]);
	    if (info->wirelessSupported[i] &&  (i >= LTE_CELL_START  && i <= LTE_CELL_END))
	    {
	       //lte
	        lteRadioConfig *lInfo = &info->capLte.lte[i];
		   
		    json_add_int(cellNumberStr, i + 1, dataRoot);
	        if (lInfo->band != 0)
	        {
	            json_add_int(bandStr, lInfo->band, dataRoot);
	        }
	        if (lInfo->ul_earfcn != 0)
	        {
	            json_add_int(ulEarfcnStr, lInfo->ul_earfcn, dataRoot);
	        }
	        if (lInfo->dl_earfcn != 0)
	        { 
	            json_add_int(dlEarfcnStr, lInfo->dl_earfcn, dataRoot);
	        }
	        if (lInfo->pci != 0)
	        {
	            json_add_int(pciStr, lInfo->pci, dataRoot);
	        }
	        if (lInfo->plmnid != 0)
	        {
	            json_add_int(plmnidStr, lInfo->plmnid, dataRoot);
	        }
	        if (lInfo->tac != 0)
	        {
	            json_add_int(tacStr, lInfo->tac, dataRoot);
	        }
	        if (lInfo->neiborcell[0] != 0)
	        {
	            json_add_string(pciStr, lInfo->neiborcell, data1Root);
	        }
	        if (lInfo->abnorfreq[0] != 0)
	        {
	            json_add_string(freqStr, lInfo->abnorfreq, data1Root);
	        }
	
	        json_object_object_add(dataRoot, neighborCellListStr, data1Root);
	        json_object_object_add(cmdRoot, dataStr, dataRoot);	
	    } 
	    if (info->wirelessSupported[i] && (i >= GSM_CELL_START  && i <= GSM_CELL_END))
	    {
	        //here is GSM
	        gsmRadioConfig *gInfo;
	        if(i == GSM_CELL_START) 
			{
			    gInfo = &info->capGsm.gsm[0];
	        }
			else
			{
			    gInfo = &info->capGsm.gsm[1];
			}
			
		    json_add_int(cellNumberStr, i + 1, dataRoot);
	        if (gInfo->band != 0)
	        {
	            json_add_int(bandStr, gInfo->band, dataRoot);
	        }
	        if (gInfo->bcc != 0)
	        {
	            json_add_int(bccStr, gInfo->bcc, dataRoot);
	        }
	        if (gInfo->mcc != 0)
	        { 
	            json_add_int(mccStr, gInfo->mcc, dataRoot);
				json_add_int(mncStr, gInfo->mnc, dataRoot);
	        }
	        if (gInfo->lac != 0)
	        {
	            json_add_int(lacStr, gInfo->lac, dataRoot);
	        }
	        if (gInfo->lac != 0)
	        {
	            json_add_int(lacStr, gInfo->lac, dataRoot);
	        }
	        if (gInfo->lowatt != 0)
	        {
	            json_add_int(lowattStr, gInfo->lowatt, dataRoot);
	        }
	        if (gInfo->upatt != 0)
	        {
	            json_add_int(upattStr, gInfo->upatt, dataRoot);
	        }
            if (gInfo->captime != 0)
            {
	            json_add_int(captimeStr, gInfo->captime, dataRoot);
            }
            if (gInfo->PowerLevel != 0)
            {
                json_add_int(powerLevelStr, gInfo->PowerLevel, dataRoot);
            }


	        json_object_object_add(cmdRoot, dataStr, dataRoot);	        
	    }

	    if (info->wirelessSupported[i] && i == GSM_CELL_END + 1)
	    {
	        //cdma
	  
	    }
	    if (info->wirelessSupported[i] && (i >= WCDMA_CELL_START  && i <= WCDMA_CELL_END))
	    {
    	    wcdmaRadioConfig *wInfo = NULL;

            if(i == WCDMA_CELL_START)
            {
	            wInfo = &info->capWcdma;
            }
            else
            {
	            wInfo = &info->capWcdma;
            }

		    json_add_int(cellNumberStr, i + 1 , dataRoot);

	        if (wInfo->mcc != 0)
	        { 
	            json_add_int(mccStr, wInfo->mcc, dataRoot);
				json_add_int(mncStr, wInfo->mnc, dataRoot);
	        }
			
	        if (wInfo->lac != 0)
	        {
	            json_add_int(lacStr, wInfo->lac, dataRoot);
	        }
			
	        if (wInfo->psc != 0)
	        {
	            json_add_int(pscStr, wInfo->psc, dataRoot);
	        }
			
	        if (wInfo->rac != 0)
	        {
	            json_add_int(lacStr, wInfo->rac, dataRoot);
	        }		
			
	        if (wInfo->cellId!= 0)
	        {
	            json_add_int(cellIdStr, wInfo->cellId, dataRoot);
	        }
			
	        json_object_object_add(cmdRoot, dataStr, dataRoot);	        
	    }
	}
	
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
   
}

bool wrapperSnifferTm(WirelessConfig *info, I8 *outdata, I32 *len)
{
    I32 i = 0;
    time_t rawtime;
    time(&rawtime); 
	
	for (;i < maxCell; i++)
	{
	    if (info->wirelessSupported[i] && (i == 7 || i == 8))
	    {
	        JSON cmdRoot = json_object_new_object();
	        JSON dataRoot = json_object_new_object();
	        JSON data1Root = json_object_new_object();
	        json_add_int(typeStr, CMD_CONFIG, cmdRoot);
	        json_add_int(codeStr, CFG_AIR_SNIFFER, cmdRoot);
			json_add_int(timestampStr, rawtime, cmdRoot);			
	        json_add_int(cellNumberStr, i, dataRoot);
			
	        SnifferConfig *sInfo = &info->snfConfig[i];
	        if (sInfo->bccMode != 0)
	        {
	            json_add_int(autoBccModeStr, sInfo->bccMode, dataRoot);
	        }
	        if (sInfo->isAuto != 0)
	        {
	            json_add_int(isAutoStr, sInfo->isAuto, dataRoot);
	        }			
	        if (sInfo->hour != 0)
	        {
	            json_add_int(hourStr, sInfo->hour, data1Root);
	        }
	        if (sInfo->min != 0)
	        {
	            json_add_int(minuteStr, sInfo->min, data1Root);
	        }
	        if (sInfo->sec != 0)
	        {
	            json_add_int(secondStr, sInfo->sec, data1Root);
	        }
	        if (sInfo->cycle != 0)
	        {
	            json_add_int(snifferCycleStr, sInfo->cycle, dataRoot);
	        }
	        json_object_object_add(dataRoot, runTimeStr, data1Root);
	        json_object_object_add(cmdRoot, dataStr, dataRoot);
	        *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
	        json_object_put(cmdRoot);
	        return true;
	    }
	}
	
    return false;

}

bool wrapperlteInitCfg(WIRELESS_INFO *info, I8 *outdata, I32 *len)
{
    I32 i = 0;
    for ( ; i < maxCell; i++ )
    {
        if (info->wirelessSupported[i] == '1')
            break;
    }
    if (i == maxCell)
        return false;
    if ( i > 6)
        return false;
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_array();
    JSON data1Root = json_object_new_object();
    json_add_int(typeStr, CMD_CONFIG, cmdRoot);
    json_add_int(codeStr, CFG_GPS_DELAY, cmdRoot);
    json_add_int(cellNumberStr, (i + 1), data1Root);
    if ( 0 <= i  && i <= 6)
    {
        LTE_INFO *lInfo = &info->capLte.lte[i];
        if (lInfo->gpsTmDelay[0] != 0)
        {
            json_add_int(GpsDelayStr, atoi(lInfo->gpsTmDelay), data1Root);
        }
        if (lInfo->IsMeasureEnable[0] != 0)
        {
            json_add_int(IsMeasureEnableStr, atoi(lInfo->IsMeasureEnable), data1Root);
        }
        if (lInfo->bandWidth[0] != 0)
        {
            json_add_int(BandWidthStr, atoi(lInfo->bandWidth), data1Root);
        }
        if (lInfo->syncMode[0] != 0)
        {
            json_add_int(SyncModeStr, atoi(lInfo->syncMode), data1Root);
        }
        if (lInfo->isSaveFs[0] != 0)
        {
            json_add_int(IsSaveFsStr, atoi(lInfo->isSaveFs), data1Root);
        }
        json_object_array_add(dataRoot, data1Root);
        json_object_object_add(cmdRoot, dataStr, dataRoot);
    }
     *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;  
}

bool wrapperFtpconfig(I32 type, I32 code, FTP_INFO *info, I8 *outdata, I32 *len)
{
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    json_add_int(typeStr, CMD_CONFIG, cmdRoot);
    json_add_int(codeStr, CFG_FTP_INFO, cmdRoot);
    if (info->ftpserver[0] != 0)
    {
        json_add_string(ftpserverStr, info->ftpserver, dataRoot);
    }
    if (info->ftpuser[0] != 0)
    {
        json_add_string(ftpuserStr, info->ftpuser, dataRoot);
    }
    if (info->ftppasswd[0] != 0)
    {
        json_add_string(ftppasswdStr, info->ftppasswd, dataRoot);
    }
    if (info->ftpport[0] != 0)
    {
        json_add_string(ftpportStr, info->ftpport, dataRoot);
    }
    if (info->ftptimer[0] != 0)
    {
        json_add_string(ftptimerStr, info->ftptimer, dataRoot);
    }
    if (info->ftpmaxcount[0] != 0)
    {
        json_add_string(ftpmaxcountStr, info->ftpmaxcount, dataRoot);
    }

    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperLteTacConfig(I32 type, I32 code, I8 *info, I8 *outdata, I32 *len)
{
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    json_add_int(typeStr, CMD_CONFIG, cmdRoot);
    json_add_int(codeStr, code, cmdRoot);

   // if(info->tac_cycle[0] != 0)
 //   {
    int value = *((int *)info);

    //cli_app_log(CLI_LOG_NOTICE, "value %d\n",value);

    char value_str[10];
    sprintf(value_str,"%d",value);

    json_add_string(tacUpdateCycleStr, value_str, dataRoot);
  //  }

    json_object_object_add(cmdRoot, dataStr, dataRoot);

    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);


     cli_app_log(CLI_LOG_NOTICE, "outdata %s\n",outdata);
    return true;
}


bool wrapperSrOneRecord(I32 type, I32 code, SEARCHRECORD *info, I8 *outdata, I32 *len)
{
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(codeStr, code, cmdRoot);
    
    json_add_int(seqNumStr, info->seqNum, dataRoot);
    
    if (info->date[0] != 0)
    {
        json_add_string(dateStr, info->date, dataRoot);
    }
    
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperOnedayRecord(I32 type, I32 code, I8 *data, I8 *outdata, I32 *len)
{
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(codeStr, code, cmdRoot);
    
    char day[12] = {0};
    if (data != NULL)
    {
        memcpy(day, data, 8);
        day[8] = '\0';
        json_add_string(dateStr, data, dataRoot);
    }
    
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperUpgradePacket(I32 type, I32 code, ZHONGDUN_PACKET *packet, I8 *outdata, I32 *len)
{
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(codeStr, code, cmdRoot);
    
    json_add_int(snStr, packet->sn, dataRoot);
    json_add_int(lengthStr, packet->len, dataRoot);
    json_add_string(packetStr, packet->data, dataRoot);
    json_add_string(crcStr, packet->crc, dataRoot);
    
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperSrRecordBySeq(I32 type, I32 code, U16 seqNum, I8 *outdata, I32 *len)
{
    JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();
    json_add_int(typeStr, type, cmdRoot);
    json_add_int(codeStr, code, cmdRoot);
    
    json_add_int(seqNumStr, seqNum, dataRoot);
    
    json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperSshOnMsg(I32 type, I32 code, REMOTE_SSH *sshinfo, I8 *outdata, I32 *len)
{
	JSON cmdRoot = json_object_new_object();
    JSON dataRoot = json_object_new_object();

	time_t ttime;
    time(&ttime);
    json_add_int(typeStr, CMD_CTRL, cmdRoot);
	json_add_int(codeStr, CT_REMOTE_SSH_ON, cmdRoot);
    json_add_int(timestampStr, ttime, cmdRoot);

	if(sshinfo->port[0] != 0)
	{
		json_add_string("port", sshinfo->port, dataRoot);
	}
	if(sshinfo->user[0] != 0)
	{
		json_add_string("user", sshinfo->user, dataRoot);
	}
	if(sshinfo->password[0] != 0)
	{
		json_add_string("password", sshinfo->password, dataRoot);
	}
	if(sshinfo->serverAddr[0] != 0)
	{
		json_add_string("serverAddr", sshinfo->serverAddr, dataRoot);
	}

	json_object_object_add(cmdRoot, dataStr, dataRoot);
    *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;
}

bool wrapperCtrlMsg(I32 code, I8 *data, I8 *outdata, I32 *len)
{
    bool ret = true;
    switch(code)
    {
        case CT_BBU_SOFT_RESTART:
            ret = wrapperwireless(CMD_CTRL, CT_BBU_SOFT_RESTART, *(I32 *)data, outdata, len);
            break;
        case CT_SLEEP:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_SLEEP, outdata, len);
            break;     
        case CT_RESRART:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_RESRART, outdata, len);
            break;
        case CT_WIRELISS_OFF:
            ret = wrapperwireless(CMD_CTRL, CT_WIRELISS_OFF, 0, outdata, len);
            break;
        case CT_WAKEUP:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_WAKEUP, outdata, len);
            break;
        case CT_WIRELISS_ON:
            ret = wrapperwireless(CMD_CTRL, CT_WIRELISS_ON, 0, outdata, len);
            break;
        case CT_SMS_OFF:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_SMS_OFF, outdata, len);
            break;
        case CT_SMS_ON:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_SMS_ON, outdata, len);
            break;
        case CT_SMS_SEND:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_SMS_SEND, outdata, len);
            break;
        case CT_SRV_UPDATA_OFF:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_SRV_UPDATA_OFF, outdata, len);
            break;
        case CT_SRV_UPDATA_ON:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_SRV_UPDATA_ON, outdata, len);
            break;
        case CT_AIR_SNIFFER_OFF:
            ret = wrapperwireless(CMD_CTRL, CT_AIR_SNIFFER_OFF, *(I32 *)data, outdata, len);
            break;
        case CT_AIR_SNIFFER_ON:
            ret = wrapperwireless(CMD_CTRL, CT_AIR_SNIFFER_ON, *(I32 *)data, outdata, len);
            break;

        case CT_TIME_SYNC:
            ret = wrapperTimeSyncMsg(CMD_CTRL,CT_TIME_SYNC, *(I32 *)data, outdata, len);
            break;
        case CT_BBU_PW_OFF:
            ret = wrapperWirelessTwoLevel(CMD_CTRL, CT_BBU_PW_OFF, *(I32 *)data, palistStr, outdata, len);
            break;
        case CT_BBU_PW_ON:
            ret = wrapperWirelessTwoLevel(CMD_CTRL, CT_BBU_PW_ON, *(I32 *)data, palistStr, outdata, len);
            break;
        case CT_RUNSTATS_ON:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_RUNSTATS_ON, outdata, len);
            break;
        case CT_RUNSTATS_OFF:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_RUNSTATS_OFF, outdata, len);
            break;
        case CT_RESETTOFACTORY:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_RESETTOFACTORY, outdata, len);
            break;
        case CT_SNIFFER:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_SNIFFER, outdata, len);
            break;
  
        case CT_SMS_MAIN_SWITCH:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_SMS_MAIN_SWITCH, outdata, len);
            break;

        case CT_CLEAN_DATA:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_CLEAN_DATA, outdata, len);
            break;
        case CT_SET_BLACKLIST_ON:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_SET_BLACKLIST_ON, outdata, len);
            break;
        case CT_SET_BLACKLIST_OFF:
            ret = wrapperSimpleMsg(CMD_CTRL,CT_SET_BLACKLIST_OFF, outdata, len);
            break;

        case CT_BBU_RESTART:
            ret = wrapperwireless(CMD_CTRL, CT_BBU_RESTART, *(I32 *)data, outdata, len);
            break;

        case CT_REMOTE_SSH_ON:
            ret = wrapperSshOnMsg(CMD_CTRL, CT_REMOTE_SSH_ON, (REMOTE_SSH *)data, outdata, len);
            break;

        case CT_REMOTE_SSH_OFF:
            ret = wrapperSimpleMsg(CMD_CTRL, CT_REMOTE_SSH_OFF, outdata, len);
            break;

        default:
            ret = false;  
            cli_app_log(CLI_LOG_WARNING, "can not find command CtrlMsg\n");
            break;
    }
    return ret;
}

bool wrapperConfigMsg(I32 code, I8 *data, I8 *outdata, I32 *len)
{
    bool ret = true;
    WirelessConfig  *wConfig = NULL;     
    WIRELESS_INFO * winfo = NULL;
    switch(code)
    {
        case CFG_BASE:
            break;

        case CFG_DATA_CLEAN:
            ret = wrapperSimpleMsg(CMD_CONFIG,code, outdata, len);
            break;

        
        case CFG_WIRELESS:
            wConfig = (WirelessConfig *)data;
            ret = wrapperwirelessCfg(wConfig, outdata, len);
            break;

        case CFG_DET_INTVL:
            ret = wrapperMsgWithData(CMD_CONFIG, code, *(I32 *)data, redectStr, outdata, len);
            break;

        case CFG_GPS_DELAY:
            winfo = (WIRELESS_INFO *)data;
            ret = wrapperlteInitCfg(winfo, outdata, len);
            break;

        case CFG_AIR_SNIFFER:
            wConfig = (WirelessConfig *)data;
            ret = wrapperSnifferTm(wConfig, outdata, len);
            break;

        case CFG_INTERFACE:
            ret = wrapperInterfaceCfg(CMD_CONFIG, code, data, outdata, len);
			break; 

        case CFG_SOFTWARE_UPDATA:
            if (data == NULL)
            {
                ret = wrapperSimpleMsg(CMD_CONFIG, CFG_SOFTWARE_UPDATA, outdata, len);        
            }
            else
            {
                ret = wrapperUpgradeCfg(CMD_CONFIG, code, data, outdata, len);
            }
            break;
        case CFG_BLACKLIST_ADD:
			ret = wrapperImsiList(CMD_CONFIG, code, data, outdata, len);
			break;			
		case CFG_BLACKLIST_DEL:
			ret = wrapperImsiList(CMD_CONFIG, code, data, outdata, len);
			break;
            
        case CFG_FTP_INFO:
            ret = wrapperFtpconfig(CMD_CONFIG, code, (FTP_INFO *)data, outdata, len);
            break;
        
        case CFG_UPDATA_TAC_CYCLE:
            cli_app_log(CLI_LOG_WARNING, "data %d\n", *((int *)data));
            ret = wrapperLteTacConfig(CMD_CONFIG, code, data, outdata, len);
            break;

        default:
            ret = false;  
            cli_app_log(CLI_LOG_WARNING, "can not find command  configMsg\n");
            break;
    }

    return ret;
}

bool wrapperSearchMsg(I32 code, I8 *data, I8 *outdata, I32 *len)
{
    bool ret = true;
    switch(code)
    {
        case SR_CURRENT_FREQ:
            break;
        case SR_BBU_CFG:
            ret = wrapperSimpleMsg(CMD_SEARCH, SR_BBU_CFG, outdata, len);
			break;
        case SR_TOTAL_CFG:
            ret = wrapperSimpleMsg(CMD_SEARCH, SR_TOTAL_CFG, outdata, len);
            break;

        case SR_FTP_INFO:
            ret = wrapperSimpleMsg(CMD_SEARCH, SR_FTP_INFO, outdata, len);
            break;

        case SR_BLACKLIST:
            ret = wrapperSimpleMsg(CMD_SEARCH, SR_BLACKLIST, outdata, len);
            break;
        case SR_DEV_VERSION:
            ret = wrapperSimpleMsg(CMD_SEARCH, SR_DEV_VERSION, outdata, len);
            break;

        case SR_X86_CURRENT_TIME:
            ret = wrapperSimpleMsg(CMD_SEARCH, SR_X86_CURRENT_TIME, outdata, len);
            break;
                 
        case SEARCH_ONE_RECORD:
            ret = wrapperSrOneRecord(CMD_SEARCH, code,  (SEARCHRECORD *)data, outdata, len);
            break;

        case SR_50_DAY_RECORD:
            ret = wrapperSimpleMsg(CMD_SEARCH, code, outdata, len);
            break;

        case SR_ONE_DAY_RECORD:
            ret = wrapperOnedayRecord(CMD_SEARCH, code, data, outdata, len);
            break;

        case SR_RECORD_BY_SEQ:
            ret = wrapperSrRecordBySeq(CMD_SEARCH, code, *(U16 *)data, outdata, len);
            break;

        case SR_RECORD_END:
            ret = wrapperSimpleMsg(CMD_SEARCH, code, outdata, len);
            break;

        case SEND_UPGRADE_PACKET:
            ret = wrapperUpgradePacket(CMD_SEARCH, code, (ZHONGDUN_PACKET *)data, outdata, len);
            break;

        case SEND_PACKET_END:
            ret = wrapperSimpleMsg(CMD_SEARCH, code, outdata, len);
            break;            

        default:
            ret = false;  
            cli_app_log(CLI_LOG_WARNING, "can not find command  searchMsg\n");
            break;
    }

    return ret;
}

bool wrapperHeartBeat(I32 type, I8 *outdata, I32 *len)
{
    JSON cmdRoot=json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(statsStr, type, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);
     *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);
    return true;      
}

bool wrapperRegSuccess(I32 type, I8 *outdata, I32 *len)
{
    JSON cmdRoot=json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(statsStr, HT_SRV_STATUS_OK, cmdRoot);
    json_add_int(timestampStr, rawtime, cmdRoot);
    json_add_int(codeStr, type, cmdRoot);
    json_add_string(msgStr, regSuccStr, cmdRoot);
     *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);

    return true;      
}

bool wrapperSrvRspMsg(I32 uptime, I8 *outdata, I32 *len)
{
    JSON cmdRoot=json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(upresultStr, 1, cmdRoot);
    json_add_int(ueIdStr, 1, cmdRoot);   
    json_add_int(srcTimeStr,uptime, cmdRoot);
    json_add_int(timeStr, rawtime, cmdRoot);
     *len = sprintf(outdata, "%s", json_object_to_json_string(cmdRoot));
    json_object_put(cmdRoot);

    return true;              
}

bool wrapperOthersMsg(I32 code, I8 *data, I8 *outdata, I32 *len)
{
    bool ret = true;
    switch(code)
    {
    
        case HT_SRV_STATUS_OK:
            ret = wrapperHeartBeat(code, outdata, len);
            break;

        case SEX_REG_SUCCESS:
            ret = wrapperRegSuccess(code, outdata, len);
            break;
     
        case SRV_RSP_MSG:
            ret = wrapperSrvRspMsg(*(I32 *)data, outdata, len);
            break;
            
        default:
            ret = false;  
            cli_app_log(CLI_LOG_WARNING, "can not find command  OthersMsg\n");
            break;

    }

    return ret;
}

bool msgConversion(I32 type, I32 code, I8 *data, I8 *outdata, I32 *len)
{
    bool ret = true;
    if ( outdata == NULL )
    {
        cli_app_log(CLI_LOG_ERROR, "outdata buf is NULL \n");
        return false;
    }
    cli_app_log(CLI_LOG_INFO, "msg conversion type is 0x%x  code is 0x%x \n", type, code);
    switch(type)
    {
        case CMD_CTRL:
            ret = wrapperCtrlMsg(code, data, outdata, len);

            break;

        case CMD_CONFIG:
            ret = wrapperConfigMsg(code, data, outdata, len);
            break;

        case CMD_SEARCH:
            ret = wrapperSearchMsg(code, data, outdata, len);
            break;
        
        case CMD_OTHERS:
            ret = wrapperOthersMsg(code, data, outdata, len);
            break;
   
        default:
            cli_app_log(CLI_LOG_WARNING, "can not find command \n");
            ret = false;
            break;

    }
    if (ret)
    {
        outdata[*len] = '\0';
        cli_app_log(CLI_LOG_INFO, "conversion DATA is %s len is %d \n", outdata, *len);
    }
    else
    {
        cli_app_log(CLI_LOG_ERROR, "conversion DATA error\n");
    }

    return ret;
}


static bool cmd_get_char_par(I8 * buf, const  I8 * string, I8 * dst)
{
    const I8 *tmp;
    I32 i;
    JSON char_par1;
    char_par1 = json_tokener_parse(buf);
    if (char_par1 == NULL) {
        cli_app_log(CLI_LOG_WARNING, "can not parse data\n");
        return IF_APP_PARSE_ERROR;
    }
    tmp = json_object_get_string(json_object_object_get(char_par1, string));
    if(tmp == NULL) {	
        json_object_put(char_par1);
        return IF_APP_ERROR;		
    }
    i = strlen(tmp);

    memcpy(dst, tmp, i);
    dst[i] = '\0';	
    json_object_put(char_par1);
    char_par1 = NULL;
    return true;
}


bool rspMsgConversion(I32 *type, I32 *code, I32 *result, I8 *indata, I8 *outdata, I32 len)
{
    I8 codebuf[DEV_INFO_NUM_LEN] = {0};
    I8 typebuf[DEV_INFO_NUM_LEN] = {0};
    I8 resultbuf[DEV_INFO_PAR_LEN] = {0};
    I8 databuf[8192] = {0};
    I8 data1buf[8192] = {0};
    WirelessConfig *config = (WirelessConfig *)outdata;
    if (strstr(indata, statsStr) != NULL)
    {
        *type = CMD_OTHERS;
        *code = HT_SRV_STATUS_OK;
        return true;
    }
    else if (strstr(indata, cmdTypeStr) != NULL && strstr(indata, resultStr) != NULL)
    {
        if (!cmd_get_char_par(indata, codeStr, codebuf))
        {
            cli_app_log(CLI_LOG_WARNING, "rsp command can not find code\n");
            return false;
        }
        *code = atoi(codebuf);
        if (!cmd_get_char_par(indata, cmdTypeStr, typebuf))
        {
            cli_app_log(CLI_LOG_WARNING, "rsp command can not find type\n");
            return false;
        }
        *type = atoi(typebuf);
        if (!cmd_get_char_par(indata, resultStr, resultbuf))
        {
            cli_app_log(CLI_LOG_WARNING, "rsp command can not find result\n");
            return false;
        }
        *result = atoi(resultbuf);
        cli_app_log(CLI_LOG_WARNING, "rsp result %d\n", *result);
        if (cmd_get_char_par(indata, dataStr, databuf))
        {
            if (*code == SR_BBU_CFG)
            {
                I8 num[DEV_INFO_NUM_LEN] = {0};
                I32 cell = 0;
                cmd_get_char_par(databuf, cellNumberStr, num);
                cell = atoi(num) - 1;
                memset(num, 0, DEV_INFO_NUM_LEN);
                config->wirelessSupported[cell] = 1;
                if (cell >= LTE_CELL_START && cell <= LTE_CELL_END )
                {                    
                    lteRadioConfig *lteInfo = &config->capLte.lte[cell];
                    cmd_get_char_par(databuf, GpsDelayStr, num);
                    lteInfo->gpsTmDelay = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, BandWidthStr, num);
                    lteInfo->bandWidth = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, SyncModeStr, num);
                    lteInfo->syncMode = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, bandStr, num);
                    lteInfo->band = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, dlEarfcnStr, num);
                    lteInfo->dl_earfcn = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, ulEarfcnStr, num);
                    lteInfo->ul_earfcn = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, pciStr, num);
                    lteInfo->pci = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, plmnidStr, num);
                    lteInfo->plmnid = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, tacStr, num);
                    lteInfo->tac = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, neighborCellListStr, data1buf);
                    cmd_get_char_par(data1buf, freqStr, lteInfo->abnorfreq);
                    cmd_get_char_par(data1buf, pciStr, lteInfo->neiborcell);
                }
                else if (cell >= GSM_CELL_START && cell <= GSM_CELL_END)
                {
                    gsmRadioConfig *gsmInfo = &config->capGsm.gsm[cell - GSM_CELL_START];
                    cmd_get_char_par(databuf, bandStr, num);
                    gsmInfo->band = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);     
                    cmd_get_char_par(databuf, bccStr, num);
                    gsmInfo->bcc = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);   
                    cmd_get_char_par(databuf, mccStr, num);
                    gsmInfo->mcc = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);       
                    cmd_get_char_par(databuf, mncStr, num);
                    gsmInfo->mnc = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN); 
                    cmd_get_char_par(databuf, lacStr, num);
                    gsmInfo->lac = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);           
                  
                }
                else if (cell >= WCDMA_CELL_START && cell <= WCDMA_CELL_END)
                {
                    wcdmaRadioConfig *wcdmaInfo;
                    if (cell == WCDMA_CELL_START)
                        wcdmaInfo = &config->capWcdma;
                    else
                        wcdmaInfo = &config->capTdscdma;
                    cmd_get_char_par(databuf, mccStr, num);
                    wcdmaInfo->mcc = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, mncStr, num);
                    wcdmaInfo->mnc = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, lacStr, num);
                    wcdmaInfo->lac = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, pscStr, num);
                    wcdmaInfo->psc = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, arfcnStr, num);
                    wcdmaInfo->arfcn = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, racStr, num);
                    wcdmaInfo->rac = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, cellIdStr, num);
                    wcdmaInfo->cellId = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                    cmd_get_char_par(databuf, autoLacStr, num);
                    wcdmaInfo->autoLac = atoi(num);
                    memset(num, 0, DEV_INFO_NUM_LEN);
                }
            }
            else if (*code == SR_FTP_INFO)
            {
                FTP_INFO *ftpInfo = (FTP_INFO *)outdata;
                cmd_get_char_par(databuf, ftpserverStr, ftpInfo->ftpserver);
                cmd_get_char_par(databuf, ftpuserStr, ftpInfo->ftpuser);
                cmd_get_char_par(databuf, ftppasswdStr, ftpInfo->ftppasswd);
                cmd_get_char_par(databuf, ftpportStr, ftpInfo->ftpport);
                cmd_get_char_par(databuf, ftptimerStr, ftpInfo->ftptimer);
                cmd_get_char_par(databuf, ftpmaxcountStr, ftpInfo->ftpmaxcount);


            }
            else if (*code == SR_BLACKLIST)
            {
                strcpy(outdata, databuf);
            }
        }
    }   
    else if (strstr(indata, ReqTypeStr) != NULL)
    {
        if (!cmd_get_char_par(indata, ReqTypeStr, typebuf))
        {
            return false;
        }
        if (!cmd_get_char_par(indata, ReqCodeStr, codebuf))
        {
            return false;
        }
        *type = atoi(typebuf);
        *code = atoi(codebuf);
        if (*code == BLACKLIST_ALERT)
        {
            cmd_get_char_par(indata, imsiStr, outdata);
        }

    }
    else if (strstr(indata, devAddresssStr) != NULL)
    {
        RegisterMsg *registeMsg=(RegisterMsg *)outdata;
        if (!cmd_get_char_par(indata, devAddresssStr, registeMsg->devAddr))
        {
            cli_app_log(CLI_LOG_WARNING, "registed msg can not find addr\n");
            return false;            
        }
        if (!cmd_get_char_par(indata, phoneNumberStr, registeMsg->phoneNum))
        {
            cli_app_log(CLI_LOG_WARNING, "registed msg can not find phoneNum\n");
            return false;  
        }
        if (!cmd_get_char_par(indata, devNameStr, registeMsg->devName))
        {
            cli_app_log(CLI_LOG_WARNING, "registed msg can not find devName\n");
            return false;  
        }
        if (!cmd_get_char_par(indata, devNumberStr, registeMsg->devNum))
        {
            cli_app_log(CLI_LOG_WARNING, "registed msg can not find devNum\n");
            return false;  
        }
        if (!cmd_get_char_par(indata, macStr, registeMsg->mac))
        {
            cli_app_log(CLI_LOG_WARNING, "registed msg can not find macss\n");
            return false;  
        }
        if (!cmd_get_char_par(indata, recCauseStr, registeMsg->recCause))
        {
            cli_app_log(CLI_LOG_WARNING, "registed msg can not find macss\n");
            return false;  
        }
        *type = CMD_OTHERS;
        *code = SEX_REG_SUCCESS;
    }
    else if (strstr(indata, resultStr) != NULL && strstr(indata, SnifferCompleteStr) != NULL)
    {
        cmd_get_char_par(indata, dataStr, databuf);
        *type = CMD_OTHERS;
        *code = SNF_RESULT;
        I32 index = 0;
        I8 cellNum[DEV_INFO_NUM_LEN] = {0};
        I8 lac[DEV_INFO_NUM_LEN] = {0};
        I8 arfcn[DEV_INFO_NUM_LEN] = {0};
        I8 bsic[DEV_INFO_NUM_LEN] = {0};
        I8 rssi[DEV_INFO_NUM_LEN] = {0};
        I8 c1[DEV_INFO_NUM_LEN] = {0};
        I8 c2[DEV_INFO_NUM_LEN] = {0};
 
        gsmSnifferResult* sniff =  (gsmSnifferResult* )outdata;
        memset(sniff, 0, sizeof(gsmSnifferResult));
        while(json_get_array(databuf, data1buf, index ))
        {
            cmd_get_char_par(data1buf, cellNumberStr, cellNum);
            cmd_get_char_par(data1buf, lacStr, lac);
            cmd_get_char_par(data1buf, arfcnStr, arfcn);
            cmd_get_char_par(data1buf, bsicStr, bsic);
            cmd_get_char_par(data1buf, rssiStr, rssi);
            cmd_get_char_par(data1buf, c1Str, c1);
            cmd_get_char_par(data1buf, c2Str, c2);
            if (atoi(cellNum) == 8)
                sprintf(sniff->cell[sniff->cellNum].wPlmnid, "%s", "46000");
            else
                sprintf(sniff->cell[sniff->cellNum].wPlmnid, "%s", "46001");
            sniff->cell[sniff->cellNum].wLAC = atoi(lac);
            sniff->cell[sniff->cellNum].wARFCN = atoi(arfcn);
            sniff->cell[sniff->cellNum].wBSIC = atoi(bsic);
            sniff->cell[sniff->cellNum].cRSSI = atoi(rssi);
            sniff->cell[sniff->cellNum].cC1 = atoi(c1);
            sniff->cell[sniff->cellNum].bC2 = atoi(c2);

            memset(cellNum, 0, DEV_INFO_NUM_LEN);
            memset(lac, 0, DEV_INFO_NUM_LEN);
            memset(arfcn, 0, DEV_INFO_NUM_LEN);
            memset(bsic, 0, DEV_INFO_NUM_LEN);
            memset(rssi, 0x0, DEV_INFO_NUM_LEN);
            memset(c1, 0x0, DEV_INFO_NUM_LEN);
            memset(c2, 0x0, DEV_INFO_NUM_LEN);
            memset(data1buf, 0, DEV_INFO_PAR_LEN);
            sniff->cellNum++;
            index++; 
        }

        cli_app_log(CLI_LOG_DEFAULT, "sniffer result complete \n");
    }
    else if (strstr(indata, wirelessSwitchStr) != NULL)
    {
        I32 index = 0;
        DevStaus *devStatus = (DevStaus *)outdata;
        for (; index < CELL_NUM; index++)
        {
            devStatus->bbu[index].active = -1; 
        }
        index = 0;
        if (cmd_get_char_par(indata, patempStr, databuf))
        {
            while (json_get_array(databuf, data1buf, index ))
            {
                if (cmd_get_char_par(data1buf, paNumberStr, codebuf))
                {
                    if (cmd_get_char_par(data1buf, tempStr, typebuf))
                    {
                        devStatus->pa[atoi(codebuf) -1].temp = atoi(typebuf);
                    }
                }
                memset(data1buf, 0, DEV_INFO_ADD_LEN);
                memset(codebuf, 0, DEV_INFO_NUM_LEN);
                memset(typebuf, 0, DEV_INFO_NUM_LEN);
                index++; 
            }
        }
        memset(databuf, 0, DEV_INFO_ADD_LEN);
        index = 0;
        if (cmd_get_char_par(indata, paPowerStr, databuf))
        {
            while (json_get_array(databuf, data1buf, index ))
            {
                if (cmd_get_char_par(data1buf, paNumberStr, codebuf))
                {
                    if (cmd_get_char_par(data1buf, powerStr, typebuf))
                    {
                        devStatus->pa[atoi(codebuf) -1].power = atoi(typebuf);
                    }
                }
                memset(data1buf, 0, DEV_INFO_ADD_LEN);
                memset(codebuf, 0, DEV_INFO_NUM_LEN);
                memset(typebuf, 0, DEV_INFO_NUM_LEN);
                index++; 
            }
        }
        memset(databuf, 0, DEV_INFO_ADD_LEN);
        index = 0;
        if (cmd_get_char_par(indata, paWaveRatioStr, databuf))
        {
            while (json_get_array(databuf, data1buf, index ))
            {
                if (cmd_get_char_par(data1buf, paNumberStr, codebuf))
                {
                    if (cmd_get_char_par(data1buf, paWaveRatioStr, typebuf))
                    {
                        devStatus->pa[atoi(codebuf) -1].pawr = atoi(typebuf);
                    }
                }
                memset(data1buf, 0, DEV_INFO_ADD_LEN);
                memset(codebuf, 0, DEV_INFO_NUM_LEN);
                memset(typebuf, 0, DEV_INFO_NUM_LEN);
                index++; 
            }
        }
        index = 0;
        memset(databuf, 0, DEV_INFO_ADD_LEN);
        if (cmd_get_char_par(indata, paSwitchStr, databuf))
        {
            while (json_get_array(databuf, data1buf, index ))
            {
                if (cmd_get_char_par(data1buf, paNumberStr, codebuf))
                {
                    if (cmd_get_char_par(data1buf, paSwitchStr, typebuf))
                    {
                        devStatus->pa[atoi(codebuf) -1].paSwicth = atoi(typebuf);
                    }
                }
                memset(data1buf, 0, DEV_INFO_ADD_LEN);
                memset(codebuf, 0, DEV_INFO_NUM_LEN);
                memset(typebuf, 0, DEV_INFO_NUM_LEN);
                index++; 
            }
        }
        index = 0;
        memset(databuf, 0, DEV_INFO_ADD_LEN);
        if (cmd_get_char_par(indata, BbuRFStateStr, databuf))
        {
            cli_app_log(CLI_LOG_DEFAULT, "rfstate is %s \n", databuf);
            while (json_get_array(databuf, data1buf, index ))
            {
                cli_app_log(CLI_LOG_DEFAULT, "rs cell is %s \n", data1buf);
                if (cmd_get_char_par(data1buf, cellNumberStr, codebuf))
                {
                    if (cmd_get_char_par(data1buf, RFSwitchStr, typebuf))
                    {
                        devStatus->bbu[atoi(codebuf) -1].active = atoi(typebuf) ;
                    }
                    cli_app_log(CLI_LOG_DEFAULT, "active bbu is %d \n", atoi(codebuf));
                }
                memset(data1buf, 0, DEV_INFO_ADD_LEN);
                memset(codebuf, 0, DEV_INFO_NUM_LEN);
                memset(typebuf, 0, DEV_INFO_NUM_LEN);
                index++; 
            }
        }
        index = 0;
        memset(databuf, 0, DEV_INFO_ADD_LEN);
        if (cmd_get_char_par(indata, syncinfoStr, databuf))
        {
            while (json_get_array(databuf, data1buf, index ))
            {
                if (cmd_get_char_par(data1buf, cellNumberStr, codebuf))
                {
                    devStatus->bbu[atoi(codebuf) -1].state = 5;
                    if (cmd_get_char_par(data1buf, syncmethodStr, typebuf))
                    {
                        devStatus->bbu[atoi(codebuf) -1].syncMethod = atoi(typebuf);
                    }
                    memset(typebuf, 0, DEV_INFO_NUM_LEN);
                    if (cmd_get_char_par(data1buf, syncstatusStr, typebuf))
                    {
                        devStatus->bbu[atoi(codebuf) -1].syncStatus = atoi(typebuf);
                    }
                }
                memset(data1buf, 0, DEV_INFO_ADD_LEN);
                memset(codebuf, 0, DEV_INFO_NUM_LEN);
                memset(typebuf, 0, DEV_INFO_NUM_LEN);
                index++; 
            }
        }
        index = 0;
        memset(databuf, 0, DEV_INFO_ADD_LEN);
        memset(codebuf, 0, DEV_INFO_NUM_LEN);
        if (cmd_get_char_par(indata, devtempStr, codebuf))
        {             
            devStatus->devTemp = atoi(codebuf);
        }
        memset(codebuf, 0, DEV_INFO_NUM_LEN);
        if (cmd_get_char_par(indata, boardtempStr, codebuf))
        {             
            devStatus->boardTemp = atoi(codebuf);
        }
        memset(codebuf, 0, DEV_INFO_NUM_LEN);
        if (cmd_get_char_par(indata, cpuStr, codebuf))
        {             
            devStatus->cpuUsage = atoi(codebuf);
        }
        memset(codebuf, 0, DEV_INFO_NUM_LEN);
        if (cmd_get_char_par(indata, memoryStr, codebuf))
        {             
            devStatus->memUsage = atoi(codebuf);
        }

        *type = CMD_OTHERS;
        *code = DEVICE_STATUS;
    }
    else if (strstr(indata, "hardWareVer") != NULL)
    {
        *type = CMD_SEARCH;
        *code = SR_DEV_VERSION;
		strcpy(outdata, indata);
    }
    
    return true;

}






